<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field.options.displayStyle"
  >
    <div class="scene-waiting-current-waiting" :style="getStyle('area')">
      <!--      {{currentPatientInfo}}-->
      <!--      {{waitPatientInfo}}-->
      <div class="patient-area clearfix">
        <div class="current-area">
          <div class="sup" :style="getStyle('winArea')">
            <ScrollX>{{ roomInfo }}</ScrollX>
          </div>
          <div class="sub">
            <div class="title" :style="getStyle('currentTitle')">
              <ScrollX>{{ optionsInfo['currTitleArea']['text'] }}</ScrollX>
            </div>
            <div class="content" :style="getStyle('currArea')">
              <p v-for="(itemInfo,index) in currentPatientInfo" :key="`${itemInfo.orderNo}-${index}`"
                 :style="getStyle('currFontArea')">
                {{ itemInfo.orderNo || '\u00a0' }}
                {{ itemInfo.patientName || '\u00a0' }}
              </p>
            </div>
          </div>
        </div>
        <div class="waiting-area"  :key="widgetKey">
          <div class="title" :style="getStyle('waitingTitle')">
            <ScrollX>{{ optionsInfo['waitArea']['text'] }}</ScrollX>
          </div>
          <vue-seamless-scroll
            class="content"
            :hoverStop="false"
            :data="waitPatientInfo"
            @ScrollEnd="handleScrollEnd"
            :class-option="classOptionPatient()"
            ref="scroll-current"
          >
            <div class="content-inside"
                 v-for="(row, index) in waitPatientInfo"
                 :key="index"
                 :data-key="index"
                 :class="{'even':((index+1)%2)===0}"
            >
              <div
                class="content-inside-child"
                v-for="(item, key) in row"
                :key="`${item.queuingId}-${key}`"
              >
                <div class="child-inside" v-if="true">
                  {{ item.orderNo || '\u00a0' }}{{ item.patientName || '\u00a0' }}
                </div>
              </div>
            </div>
          </vue-seamless-scroll>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import vueSeamlessScroll from 'vue-seamless-scroll';
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";
import {v4 as uuid} from 'uuid'
let buildParent = buildParentApi();
export default {
  name: "scene-wait-single-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    ScrollX,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object()
    }
  },

  computed: {
    // 滚动配置项
    classOptionPatient() {
      return function () {
        const {waitArea = {}} = this.optionsInfo;
        const {openScroll = false, step = 1, minLens = 6} = waitArea
        let fontSize = parseFloat(document.documentElement.style.fontSize);
        let scale = (fontSize / 19.2);
        return {
          step: (fontSize / 19.2) * step * scale, // 数值越大速度滚动越快
          hoverStop: false,
          openTouch: false,
          direction: 1, // 0向下 1向上 2向左 3向右
          limitMoveNum: (openScroll ? minLens : 9999), // 开始无缝滚动的数据量
        };

      }
    },
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || {};
        let Obj = Object();
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            break;
          case "winArea":
            Reflect.set(Obj, 'fontSize', pxToVw((info.winArea.fontSize) || 50));
            Reflect.set(Obj, 'color', (info.winArea.color));
            Reflect.set(Obj, 'backgroundColor', (info.winArea.bgColor));
            break;
          case "currentTitle":
            Reflect.set(Obj, 'fontSize', pxToVw((info.currTitleArea.fontSize) || 50));
            Reflect.set(Obj, 'color', (info.currTitleArea.color));
            Reflect.set(Obj, 'backgroundColor', (info.currTitleArea.bgColor));
            break;
          case 'currArea':
            Reflect.set(Obj, 'color', (info.currArea.color));
            Reflect.set(Obj, 'backgroundColor', (info.currArea.bgColor));
            break;
          case 'currFontArea':
            Reflect.set(Obj, 'fontSize', pxToVw((info.currArea.fontSize) || 50));
            break;
          case "waitingTitle":
            Reflect.set(Obj, 'fontSize', pxToVw((info.waitArea.fontSize) || 50));
            Reflect.set(Obj, 'color', (info.waitArea.color));
            Reflect.set(Obj, 'backgroundColor', (info.waitArea.bgColor));
            break;
        }
        return Obj;
      }
    },
    //  队列-患者-数据
    doctorPatientListInfo() {
      let data;
      let devType = (this.devType);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        data = (WaitingPatientList.apiData)
      } else {
        data = (WaitingPatientList.mockData)
      }
      return data || [{waitPatientList: []}]
    },
    roomInfo() {
      const info = this.optionsInfo || {};
      const list = this.doctorPatientListInfo || [];
      const item = this.$lodash.uniqBy(list, 'workerId')[0];
      return item ? item[info['roomNameKey']] || '' : '';
    },

    currentPatientInfo() {
      let list = (this.doctorPatientListInfo);
      let summary = this.$lodash.map(list, "treatingPatient");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, 'queuingId');
      return uniqByArr.slice(0, 2);
    },


    waitPatientInfo() {
      let list = (this.doctorPatientListInfo);
      let summary = this.$lodash.map(list, "waitPatientList");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, 'queuingId');
      const lens = 15;
      if (uniqByArr.length > lens) {
        return this.$lodash.chunk(uniqByArr, 3)
      } else {
        const fill = Array.from({length: lens - uniqByArr.length}, () => ({}));
        const arr = [...uniqByArr, ...fill];
        return this.$lodash.chunk(arr, 3)
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    handleScrollEnd() {
      this.widgetKey = uuid();
    },
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-患者信息', key: key, data: data}));
          this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
/*清除浮动*/
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
  clear: both;
}

.clearfix {
  zoom: 1;
  overflow: hidden;
}

.scene-waiting-current-waiting {
  * {
    box-sizing: border-box;
  }

  padding: 25px 35px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  color: #333;
  height: auto;
  font-size: 50px;
  background: #E5EAEF;

  .patient-area {
    overflow: inherit;
    border-radius: 32px 32px;
    overflow: hidden;
    background: transparent;
    height: 100%;
    .current-area {
      float: left;
      width: 594px;
      overflow: hidden;

      .sup {
        height: 255px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #65C4CC;
        border-radius: 20px;
      }

      .sub {
        border-radius: 20px;
        overflow: hidden;
        margin-top: 25px;

        .title {
          color: white;
          font-size: 40px;
          height: 160px;
          width: 100%;
          text-align: center;
          box-sizing: border-box;
          background: rgba(220, 67, 25, 1);
          display: flex;
          padding: 0 20px;
          justify-content: center;
          align-items: center;

          .scrollText {
            width: 100%;
          }
        }

        .content {
          display: flex;
          flex-direction: column;
          flex: 1;
          height: 310px;
          align-self: center;
          justify-content: center;
          align-items: center;
          background: #fff;
          color: #DC4319;

          p {
            text-align: center;
            line-height: 1.5;
            @include singleEllipsis(100%);
          }
        }
      }

    }

    .waiting-area {
      margin-left: 620px;
      display: flex;
      flex-direction: column;
      border-radius: 24px 24px;
      overflow: hidden;
      height: 100%;
      .title {
        text-align: center;
        font-size: 50px;
        height: 158px;
        width: 100%;
        color: #0059B3;
        border-bottom: 2px solid #DDDDDD;
        @include fontWeight();
        display: flex;
        padding: 0 20px;
        align-self: center;
        align-items: center;
        justify-content: center;

        .scrollText {
          width: 100%;
          height: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          align-self: center;
        }
      }

      .content {
        background: white;
        overflow: hidden;
        height: calc(100% - 158px);
        .content-inside {
          display: flex;
          flex-wrap: wrap;

          .content-inside-child {

            font-weight: 400;
            text-align: center;
            display: flex;
            flex-basis: calc(100% / 3);
            flex-shrink: 0;
            justify-content: center;
            align-self: center;
            align-items: center;
            height: 118px;
            font-size: 54px;
            color: #333333;
            overflow: hidden;

            .child-inside {
              padding: 0 24px;
              @include singleEllipsis(100%)
            }
          }
        }
      }

      .even {
        background: rgba(244, 244, 244, 1);
      }
    }
  }


}
</style>
